package logic

import (
	"context"
	"yuekaoHouse/house_srv/model/mysql"
	"yuekaoHouse/house_srv/model/redis"

	"yuekaoHouse/house_srv/house"
	"yuekaoHouse/house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListLogic) HouseList(in *house.HouseListRequest) (*house.HouseListResponse, error) {
	// todo: 房源列表

	h := mysql.House{}
	list, err := h.HouseFindListByTitle(in.Title, in.Address, int(in.Page), int(in.Size))
	if err != nil {
		logx.Error("查询失败")
		return nil, err
	}
	var sel []*house.Houses
	for _, v := range list {
		sel = append(sel, &house.Houses{
			Title:   v.Title,
			Price:   float32(v.Price),
			Address: v.Address,
			MiaJi:   float32(v.MiaJi),
			Avatar:  v.Avatar,
			Mobile:  v.Mobile,
		})
	}
	//缓存房源数据
	redis.RedisHouseList(in.Title, sel)

	return &house.HouseListResponse{HouseList: sel}, nil
}
